<?php

/**
* @current userinfo.php
* @author   LiuYang <liu_yang@haibao.com>
* @date 2016年4月27日 上午10:01:25
*/

namespace haibao\user\data;

class UserInfo extends \haibao\user\data\BaseMysql {
    private $userProfiles = array();
	
	public function __construct() {
        parent::__construct ( '\haibao\user\model\data\UserAttentionRecord' );
        $this->setConfig( \haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_USER );
    }

    public function updateBackground($userId,$imageId,$imageUrl){
    	$sql = 'update UserProfiles set BackImageId=%s,BackPicUrl=%s where UserId=%s';
    	$this->query($sql,array($imageId,$imageUrl,$userId));
    }
    
    public function getAttentionCount($userId){
    	$sql = 'select count(*) num from UserAttentionRecord where Status=%s and UserId=%s and UserType=%s';
    	$result = $this->query($sql,array(\haibao\user\model\data\UserAttentionRecord::STATUS_ISOK,$userId,\haibao\user\model\data\UserAttentionRecord::USERTYPE_USER));
    	$row = $result->fetch_assoc();
    	$attention = $row['num'] ? $row['num'] : 0;
    	
    	$sql = 'select count(*) num from UserAttentionRecord where Status=%s and AttentionUserId=%s and UserType=%s';
    	$result = $this->query($sql,array(\haibao\user\model\data\UserAttentionRecord::STATUS_ISOK,$userId,\haibao\user\model\data\UserAttentionRecord::USERTYPE_USER));
    	$row = $result->fetch_assoc();
    	$fans = $row['num'] ? $row['num'] : 0;
    	
    	return array($fans,$attention);
    }
    
    public function getUserScore($userId){
    	$this->setConfig( \haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_DZ3 );
    	$sql = 'select extcredits2 from hb_common_member_count where uid=%s';
    	$result = $this->query($sql,array($userId));
    	$row = $result->fetch_assoc();
    	$score = $row['extcredits2'] ? $row['extcredits2'] : 0;
    	
    	$this->setConfig( \haibao\classlibrary\www\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_USER );
    	
    	return $score;
    }
    
    public function getUserLogin($userId){
    	$data = array();
    	$sql = 'select * from UserLogin where UserId=%s Order By LastLoginTime Desc Limit 1';
    	$result = $this->query($sql,array($userId));
    	$row = $result->fetch_assoc();
    	if ($row){
    		$data = new \haibao\user\model\data\UserLogin();
    		$data->loadArray($row);
    	}  	
    	
    	return $data;
    }
    
    public function delMessageBoard($model){
    	$this->beginTranscation();
    	try{
    		if (!$model->ReplyId){
    			$sql = 'delete from UserMessageBox where ReplyId=%s';
    			$this->query($sql,array($model->Id));
    		}
    		
    		$sql = 'delete from UserMessageBox where Id=%s';
    		$this->query($sql,array($model->Id));
    	}catch (\Exception $e){
    		$this->rollback();
    		throw $e;
    	}
    	
    	$this->endTranscation();
    }
    
    public function getUserProfileById($id){
    	$data = null;
    	$sql = 'select * from UserProfiles where UserId=%s';
    	$result = $this->query($sql,array($id));
    	$row = $result->fetch_assoc();
		if ($row){
			$data = new \haibao\user\model\data\UserProfiles();
			$data->loadArray($row);
		}
    	
    	return $data;
    }
    
    public function setBackGround($userId,$imageId,$imageUrl){
    	if ($userId){
    		$sql = 'update UserProfiles set BackImageId=%s,BackPicUrl=%s where UserId=%s';
    		$this->query($sql,array($imageId,$imageUrl,$userId));
    	}
    }
    
    public function addVisitCount($userId){
    	$sql = 'select ReaderCount from UserProfiles where UserId=%s';
    	$result = $this->query($sql,array($userId));
    	$row = $result->fetch_assoc();
    	$count = intval($row['ReaderCount']);
    	$sql = 'update UserProfiles set ReaderCount='.++$count.' where UserId=%s';
    	$this->query($sql,array($userId));
    	$cache = new \haibao\user\cache\UserInfo();
    	$cache->addTodayGuestCount($userId);
    }
    
    public function saveLastGuest($userId,$guestInfo){
    	$cache = new \haibao\user\cache\UserInfo();
    	
    	$cache->saveLastGuest($userId,$guestInfo);
    }
    
   	public function getLastGuest($userId){
   		$cache = new \haibao\user\cache\UserInfo();
   		
   		return $cache->getLastGuest($userId);
   	}
   	
   	public function setTodayGuestCount($userId){
   		$cache = new \haibao\user\cache\UserInfo();
   		 
   		$cache->setTodayGuestCount($userId);
   	}
   	
   	public function getTodayGuestCount($userId){
   		$cache = new \haibao\user\cache\UserInfo();
   		
   		return $cache->getTodayGuestCount($userId);
   	}
   	
   	public function getBloggerByTime($userId){
   	    $this->setConfig( \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_DZ3 );
   	    //$sql = 'select DISTINCT(uid),username,dateline from `hb_home_blog` where uid <> %s  order by dateline desc limit 5';
   	    $sql = 'select uid from hb_home_blog where uid <> %s order by dateline desc limit 10';
   	    $result = $this->query($sql,array($userId));
   	    $temp = $tempRow = $blogList = array();
   	    while ($result->fetch_assoc()){
   	        $temp[] = $result->getData('uid');
   	    }
   	    $temp = array_unique($temp);
   	    if(count($temp)>5){
   	        $temp = array_slice($temp,0,5,true);
   	    }
   	    if($temp){
   	        $sql = 'select uid,username,dateline from hb_home_blog where uid in ('.implode(',',$temp).') order by dateline desc';
   	        $resultRow = $this->query($sql,$temp);
   	        while ($resultRow->fetch_assoc()){
   	            $tempRow['uid'] = $resultRow->getData('uid');
   	            $tempRow['username'] = $resultRow->getData('username');
   	            $blogList[$resultRow->getData('uid')] = $tempRow;
   	        }
   	    }
   	    $this->setConfig( \haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_MYSQL_HAIBAO_USER );
   	    return $blogList;
   	}
   	
   	public function getUserByLoginTime($userId){
   	    //$sql = 'select DISTINCT(UserId),LoginName,LastLoginTime from UserLogin where IsActive = 1 and UserId <> %s  order by LastLoginTime desc limit 5';
   	    $sql = 'select UserId from UserLogin where IsActive = 1 and UserId <> %s  order by LastLoginTime desc limit 10';
   	    $result = $this->query($sql,array($userId));
   	    $temp = $tempRow = $userList = array();
   	    while ($result->fetch_assoc()){
   	        $temp[] = $result->getData('UserId');
   	    }
   	    $temp = array_unique($temp);
   	    if(count($temp)>5){
   	        $temp = array_slice($temp,0,5,true);
   	    }
   	    if($temp){
   	        $sql = 'select UserId,LoginName from UserLogin where UserId in ('.implode(',',$temp).') order by LastLoginTime desc';
   	        $resultRow = $this->query($sql,$temp);
   	        while ($resultRow->fetch_assoc()){
   	            $tempRow['UserId'] = $resultRow->getData('UserId');
   	            $tempRow['LoginName'] = $resultRow->getData('LoginName');
   	            $userList[$resultRow->getData('UserId')] = $tempRow;
   	        }
   	    }
        return $userList;
   	}


}
